package com.ruoyi.manage.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ListUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.manage.enterprise.domain.EcEnterpriseCheck;
import com.ruoyi.manage.enterprise.domain.EnterpriseUser;
import com.ruoyi.manage.enterprise.domain.SysUserExpand;
import com.ruoyi.manage.enterprise.mapper.SysUserExpandMapper;
import com.ruoyi.manage.enterprise.service.ISysUserExpandService;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 系统用户扩展Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-13
 */
@Service
public class SysUserExpandServiceImpl extends ServiceImpl<SysUserExpandMapper, SysUserExpand> implements ISysUserExpandService {


    @Override
    public SysUserExpand queryGetById(Long id) {
        SysUserExpand sysUserExpand = this.getById(id);
        return sysUserExpand;
    }

    /**
     * 新增系统用户扩展
     *
     * @param sysUserExpand 系统用户扩展
     * @return 结果
     */
    @Override
    public boolean addSysUserExpand(SysUserExpand sysUserExpand) {
        boolean save = this.save(sysUserExpand);
        return save;
    }

    /**
     * 修改系统用户扩展
     *
     * @param sysUserExpand 系统用户扩展
     * @return 结果
     */
    @Override
    public boolean updateSysUserExpand(SysUserExpand sysUserExpand) {
        return this.updateById(sysUserExpand);
    }

    /**
     * 批量删除系统用户扩展
     *
     * @param ids 需要删除的系统用户扩展主键
     * @return 结果
     */
    @Override
    public boolean deleteSysUserExpandByIds(Long[] ids) {
        return this.removeByIds(ListUtil.toList(ids));
    }


    /**
    * 关联企业用户
    * */
    @Override
    public boolean connectSysUserEnterprise(EcEnterpriseCheck enterpriseCheck) {
        SysUser user = SecurityUtils.getLoginUser().getUser();

        SysUserExpand userExpand = SysUserExpand.builder()
                .userType(user.getUserType())
                .relatedId(enterpriseCheck.getId())
                .userId(user.getUserId())
                .mainSign(0)
                .city(enterpriseCheck.getCityName())
                .cityCode(enterpriseCheck.getCityCode())
                .province(enterpriseCheck.getProvinceName())
                .provinceCode(enterpriseCheck.getProvinceCode())
                .county(enterpriseCheck.getAreaAdress())
                .countyCode(enterpriseCheck.getAreaCode())
                .relatedRemark(enterpriseCheck.getCompanyName())
                .registeredCompany(enterpriseCheck.getCompanyName())
                .enterpriseCreditCode(enterpriseCheck.getCreditCode())
                .enabled(1)
//                设置为普通企业因为注册的时候是主企业
                .mainSign(0)
                .build();

        boolean save = this.save(userExpand);

        return save;
    }

    @Override
    public List<EnterpriseUser> selectEnterpriseUser(Wrapper ew) {
        return baseMapper.selectEnterpriseUser(ew);
    }

}
